home *** CD-ROM | disk | FTP | other *** search
/ Experimental BBS Explossion 3 / Experimental BBS Explossion III.iso / c / cp1.zip / MYSTREAM.CPP < prev    next >
C/C++ Source or Header  |  1993-06-14  |  4KB  |  156 lines

  1. ===========================================================================
  2.  BBS: The Abacus * HST/DS * Potterville MI
  3. Date: 06-13-93 (12:21)             Number: 139
  4. From: DAVID NUGENT                 Refer#: NONE
  5.   To: ALL                           Recvd: NO  
  6. Subj: [08 of 12] Mystream.cpp        Conf: (37) C++ Langua
  7. ---------------------------------------------------------------------------
  8. // Mystream.cpp
  9. // Implementation of ios interface classes for Myio
  10. //
  11. // Written by David L. Nugent
  12. //
  13.  
  14. # include <iostream.h>
  15. # include "Mystream.h"
  16. # if defined(_MSC_VER)
  17. #  include <memory.h>
  18. # else
  19. #  include <stdlib.h>
  20. # endif
  21.  
  22.     // Mystreambuf constructor
  23.     // This simply initialises the base class streambuf
  24.     // (it is initially empty with no buffer allocated)
  25.     // and register the Myio object
  26.     // Note: we _could_ set the stream unbuffered here,
  27.     // which is useful for stdio handles, so that the
  28.     // streambuf functions overflow() and underflow()
  29.     // get called on every character rather than when
  30.     // the streambuf buffer is full
  31.  
  32. Mystreambuf::Mystreambuf (Myio * mPtr)
  33.     : streambuf (), mptr(mPtr)
  34. {
  35. //  unbuffered(1);  // Uncomment to make unbuffered
  36. }
  37.  
  38.     // Mystreambuf()
  39.     // Called when streambuf owned buffer is full
  40.     // or when stream is flushed
  41.     // Our job here is to empty the streambuf
  42.     // write buffer and reset the 'put' pointers.
  43.  
  44. int
  45. Mystreambuf::overflow (int c)
  46. {
  47.     int written;
  48.  
  49.         // Handle unbuffered stream
  50.  
  51.     if (unbuffered())       // Handle the simple case first
  52.     {
  53.         if (c == EOF)   // Special case, this only flushes
  54.             return 0;
  55.         char ch = char(c);  // Write the byte directly
  56.         written = mptr->write (&ch, 1);
  57.         return (written) ? c : EOF;
  58.     }
  59.  
  60.         // Handle buffered stream
  61.  
  62.     if (!base())        // Need to allocate a buffer
  63.         allocate();
  64.  
  65.     if (base())         // Test for memory allocation error
  66.     {
  67.         char * ep = base() + (blen() / 2);
  68.         if (!pbase())   // Set put pointers if not set up
  69.             setp (base(), ep);
  70.         int bytes = pptr() - pbase();   // Bytes to write
  71.         if (bytes)
  72.         {
  73.             written = mptr->write (pbase(), bytes);
  74.             if (!written)
  75.                 return EOF;
  76.             bytes += written;
  77.             if (bytes)  // Some is still waiting to be written
  78.                 memcpy (base(), base() + written, bytes);
  79.         }
  80.         setp (base() + bytes, ep);  // Reset 'put' pointers
  81.         return (c == EOF) ? 0 : sputc (c);  // Put pending chr in buf
  82.     }
  83.     return EOF;
  84. }
  85.  
  86.     //
  87.     // underflow() indicates that the input queue
  88.     // is empty and needs more data
  89.     //
  90.  
  91. int
  92. Mystreambuf::underflow (void)
  93. {
  94.     int bytes;
  95.  
  96.         // Handle an unbuffered stream
  97.  
  98.     if (unbuffered())
  99.     {
  100.         bytes = mptr->read (&_back[1], 1);
  101.         if (!bytes)
  102.         {
  103.             setg (0, 0, 0);
  104.             return EOF;
  105.         }
  106.         setg (_back, _back + 1, _back + 2);
  107.         return (unsigned char)_back[1];
  108.     }
  109.  
  110.         // Handle a buffered stream
  111.  
  112.     if (!base())        // Need to allocate a buffer
  113.         allocate();
  114.  
  115.     if (base())
  116.     {
  117.         char * gp = base() + blen() / 2;
  118.         if (gptr() >= egptr())
  119.         {                   // Read into the buffer from stream
  120.             overflow ();    // Flush output in case we need it
  121.             bytes = mptr->read (gp + 1, blen() / 2 - 1);
  122.             setg (gp, gp + 1, gp + bytes + 1);
  123.         }
  124.         if (gptr() < egptr())   // Get from buffer
  125.             return (unsigned char) *gptr();
  126.     }
  127.     return EOF;
  128. }
  129.  
  130.     //
  131.     // sync() needs to empty both put and get
  132.     // buffered. It will do this by calling
  133.     // overflow and simply resetting the get
  134.     // pointers to their default location.
  135.     //
  136.  
  137. int
  138. Mystreambuf::sync (void)
  139. {
  140.     if (!unbuffered())
  141.     {
  142.         overflow ();                // Force output
  143.         char * gp = base();
  144.         setp (gp, gp + blen() / 2);
  145.         gp = base() + blen() / 2;
  146.         setg (0, 0, 0);
  147.     }
  148.     return 0;
  149. }
  150.  
  151. --- MaltEd 1.0.b5
  152.  * Origin: Unique Computing Pty Ltd (3:632/348)
  153. SEEN-BY: 1/211 11/2 4 13/13 101/1 109/25 114/5 123/19 124/1 153/752 154/40
  154. SEEN-BY: 154/77 157/110 159/100 125 140 180 270 430 575 950 203/23 209/209
  155. SEEN-BY: 261/1023 280/1 390/1 396/1 5 15 2430/1 2440/5 3603/20
  156.